﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CE.Database;
using System.Data;
using CE.Global;
using System.Collections;

public partial class carinsurance_offline_CallBackList : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "201")
        {
            Response.Redirect("~/carinsurance/PendingApplication.aspx");
        }

        if (!Page.IsPostBack)
        {
            RefreshGridView();
        }
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        RefreshGridView();
    }

    private void RefreshGridView()
    {
        DataTable _dtSummary = new DataTable();
        MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
        string _spName = "[sprocCXCRMAgentCallBackDetails]";
        SortedList _sPara = new SortedList();

        _sPara.Add("@AgentID", Session[General.UserId].ToString());
        _sPara.Add("@Isnotify", 1);

        _dtSummary = _db.GetDataTableBySP(_spName, _sPara);

        GridView1.DataSource = _dtSummary;
        GridView1.DataBind();
        if (GridView1.Rows.Count > 0)
        {
            lblRecordCount.Text = "(" + GridView1.Rows.Count.ToString() + ") record(s). " + Resources.General.DateTime + ": " + dateTimeNow.ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;"; //DateTime.Now.ToString("dd-MMM-yyyy hh:mm tt")
        }
        else
        {
            lblRecordCount.Text = "(" + GridView1.Rows.Count.ToString() + ") record(s). " + Resources.General.DateTime + ": " + dateTimeNow.ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;"; //DateTime.Now.ToString("dd-MMM-yyyy hh:mm tt")
        }
    }

    protected void ddlOrderBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnRefresh_Click(null, null);
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hplCallNow = (HyperLink)e.Row.FindControl("hplCallNow");

            Label lblAgentLeadID = (Label)e.Row.FindControl("lblAgentLeadID");
            Label lblCustomerID = (Label)e.Row.FindControl("lblCustomerID");
            Label lblCarID = (Label)e.Row.FindControl("lblCarID");
            Label lblCallTypeID = (Label)e.Row.FindControl("lblCallTypeID");

            //Literal lblCallTypeDesc = (Literal)e.Row.FindControl("lblCallTypeDesc");
            Literal lblCallStatusDesc = (Literal)e.Row.FindControl("lblCallStatusDesc");
            string leadID = DataBinder.Eval(e.Row.DataItem, "AgentLeadID").ToString();
            lblAgentLeadID.Text = leadID.ToString();
            string callType = DataBinder.Eval(e.Row.DataItem, "CallType").ToString();
            lblCallTypeID.Text = callType;
            string carID = DataBinder.Eval(e.Row.DataItem, "CarID").ToString();
            lblCarID.Text = carID;
            string customerID = DataBinder.Eval(e.Row.DataItem, "CustomerID").ToString();
            lblCustomerID.Text = customerID;
            string callStatus = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
            hplCallNow.NavigateUrl = "ViewCINoMatchFound.aspx?leadid=" + leadID + "&calltype=" + callType + "&carid=" + carID + "&custid=" + customerID;

            //switch (callType)
            //{
            //    case "100":
            //        lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.FirstFollowUpCall + "</span>";
            //        break;
            //    case "101":
            //        lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallMeRequest + "</span>";
            //        break;
            //    case "102":
            //        lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallMeBack + "</span>";
            //        break;
            //    case "103":
            //        lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallDirect + "</span>";
            //        break;
            //    default: lblCallTypeDesc.Text = "N/A"; break;
            //}

            //switch (callStatus)
            //{
            //    case "1":
            //        lblCallStatusDesc.Text = "<span style='color:red;'>" + Resources.General.Pending + "</span>";
            //        break;
            //    case "2":
            if (DataBinder.Eval(e.Row.DataItem, "countback").ToString() == "")
            {
                lblCallStatusDesc.Text = Resources.General.Callback + " <span style='color:red;'>[" + Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "CallBackDateTime").ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "]</span>";
            }
            else
            {
                int countBack = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "countback"));
                if (countBack <= 0)
                {
                    countBack = Math.Abs(countBack);
                }
                else
                {
                    countBack = -(countBack);
                }
                lblCallStatusDesc.Text = Resources.General.Callback + " <span style='color:red;'>[" + Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "CallBackDateTime").ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; เหลืออีก " + countBack.ToString() + " วัน</span>";
            }
            //        break;
            //    case "3":
            //        lblCallStatusDesc.Text = "<span style='color:green;'>" + Resources.General.FollowUp + "</span>";
            //        break;
            //    default:
            //        lblCallStatusDesc.Text = "N/A";
            //        break;
            //}

            string callTypeID = DataBinder.Eval(e.Row.DataItem, "CallStatusTypeID").ToString();
            if (callTypeID == "29")
            { //Complete Soon
                e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFF7F7");
            }
            else if (callTypeID == "26")
            { //Callback, Will consider
                e.Row.BackColor = System.Drawing.Color.LightYellow;
            }
            else if (callTypeID == "27")
            { //Not yet presented 
                e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#E1FFE1"); 
            }
            else if (callTypeID == "10")
            { //Policy not expiring too soon (> 60 days) 
                e.Row.BackColor = System.Drawing.Color.Honeydew;
            }
        }
    }
}
